package jp.go.aist.six.test.util.castor.persist;

import java.util.Vector;
import jp.go.aist.six.util.castor.CastorPersistable;


public class Category
extends CastorPersistable<Integer>
{
    private int      _id;

    private final Vector<Product>   _products = new Vector<Product>();

    private String   _name;


    public Category()
    {
    }


    public Category(
                    final int id,
                    final String name
                    )
    {
        setId( id );
        setName( name );
    }


    public int getId()
    {
        return _id;
    }


    public void setId( final int id )
    {
        _id = id;
    }


    public String getName()
    {
        return _name;
    }


    public void setName( final String name )
    {
        _name = name;
    }


    //public Enumeration getProducts()
    public Vector<Product> getProducts()
    {
        return _products;
        // return _products.elements();
    }


    public void addProduct( final Product product )
    {
        if ( ! _products.contains( product ) ) {
            System.out.println( "Adding product " + product + " to category " + this );
            _products.addElement( product );
            product.addCategories( this );
        }
    }


    @Override
    public String toString()
    {
        return "Category-" + System.identityHashCode( this )
                        + "<id: " + _id + " name: " + _name + ">";
    }


    // AbstractPersistable //

    @Override
    public void setPersistentID(
                    final Integer id
                    )
    {
        if (id != null) {
            setId( id.intValue() );
        }
    }


    @Override
    public Integer getPersistentID()
    {
        return (new Integer( getId() ));
    }

}
